<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\AppointDoneAction;
use App\Admin\Repositories\Appoint;
use App\Admin\Tools\AppointDoneTool;
use App\Models\Appoint as AppointModels;
use App\Models\Family;
use App\Models\Hospital;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointType;
use App\Models\old\AppointAdult;
use App\Models\old\ChildInfo;
use App\Models\old\UserLogin;
use App\Models\Vaccine;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class AppointController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $types = HospitalAppointType::query()->pluck('name','id')->toArray();
        return Grid::make(new \App\Hospital\Repositories\Appoint(['family','hospital']), function (Grid $grid) use($types) {
            $grid->column('id');
            $grid->column('family.name','预约人姓名')->display(function ($name){
                if(!$this->family_id && $this->old_family_id){
                    if($this->type == 13){
                        $adult = AppointAdult::query()->where('id',$this->old_family_id)->first();
                        return $adult->name;

                    }else {
                        $child = ChildInfo::query()->where('id', $this->old_family_id)->first();
                        return $child->name;
                    }
                }else{
                    return $name;
                }
            });
            $grid->column('family.birthday','生日')->sortable()->display(function ($name){
                if(!$this->family_id && $this->old_family_id){
                    if($this->type == 13){
                        $adult = AppointAdult::query()->where('id',$this->old_family_id)->first();
                        return $adult->birthday;
                    }else {
                        $child = ChildInfo::query()->where('id', $this->old_family_id)->first();
                        return date('Y-m-d', $child->birthday);
                    }
                }else{
                    return $name;
                }
            });
            $grid->column('family.gender','性别')->using([0=>'未设置',1=>'男',2=>'女'])->display(function ($name){
                if(!$this->family_id && $this->old_family_id){
                    if($this->type == 13){
                        $adult = AppointAdult::query()->where('id',$this->old_family_id)->first();
                        return $adult->gender == 1 ? '男' : '女';

                    }else {
                        $child = ChildInfo::query()->where('id', $this->old_family_id)->first();
                        return $child->gender == 1 ? '男' : '女';
                    }
                }else{
                    return $name;
                }
            });;


            $grid->column('type')->using($types);
            $grid->column('project')->display(function (){
                if($this->type==2){
                    $vaccine = Vaccine::query()->where('id',$this->project)->first();
                    return isset($vaccine)?$vaccine->name:'';
                }else{
                    return "";
                }
            });
            $grid->column('hospital.name','医院');
            $grid->column('source')->using(\App\Models\Appoint::$sourceTxt);
            $grid->column('id_card');
            $grid->column('appoint_user_id');
            $grid->column('cancel_appoint_user_id');
            $grid->column('cancel');
            $grid->column('appoint_date');
            $grid->column('appoint_time')->using(HospitalAppointInterval::query()->select(DB::raw("CONCAT_WS('-', start, end) as text,id"))->pluck('text','id')->toArray());
            $grid->column('status','预约状态')->using(AppointModels::$stateTxt);
            $grid->column('created_at');

            $grid->actions(function ($actions) use ($grid){
                $actions->append(new AppointDoneAction());
                //$actions->append(new VaccineIntervalAction());
            });
            $grid->batchActions([
                new AppointDoneTool('标记完成', 1),
            ]);

            $titles = [
                'name'=>'姓名',
                'sex'=>'性别',
                'birthday'=>'生日',
                'id_card'=>'身份证号',
                'phone'=>'联系电话',
                'area'=>'户籍地',
                'appoint_date'=>'预约日期',
                'appoint_time'=>'预约时间',
                'status'=>'预约状态',
                'type'=>'预约项目',
                'hospital'=>'预约社区',
                'vaccine'=>'预约疫苗'];

            $grid->export($titles)->rows(function ($rows) {
                foreach ($rows as $index => &$row) {

                    if($row['type'] == 13){
                        $family = AppointAdult::query()->where('id',$row['old_family_id'])->first();

                        $row['name'] = $family ? $family->name : '';
                        $row['sex'] = $family ? Family::$gender[$family->gender] : '';
                        $row['birthday'] = $family ? $family->birthday : '';
                        $row['id_card'] = $family ? $family->id_card : '';
                        $row['phone'] = $family ? $family->phone : '';
                        $row['area'] = $family ? $family->address : '';
                    }else {
                        if(!$row['family_id'] && $row['old_family_id']){
                            $family = ChildInfo::query()->where('id', $row['old_family_id'])->first();
                            $row['name'] = $family ? $family->name : '';
                            $row['sex'] = $family ? Family::$gender[$family->gender] : '';
                            $row['birthday'] = $family ? date('Y-m-d', $family->birthday) : '';
                            $row['id_card'] = $family ? $family->idcard : '';
                            $userLogin = UserLogin::query()->where('userid',$family->userid)->first();
                            $row['phone'] = $userLogin ? $userLogin->phone : '';
                            $row['area'] = '';
                        }else {
                            $family = Family::query()->where('id', $row['family_id'])->first();
                            $row['name'] = $family ? $family->name : '';
                            $row['sex'] = $family ? Family::$gender[$family->gender] : '';
                            $row['birthday'] = $family ? $family->birthday : '';
                            $row['id_card'] = $family ? $family->idcard : '';
                            $row['phone'] = $family ? $family->phone : '';
                            $row['area'] = $family ? $family->address : '';
                        }
                    }
                    $interval = HospitalAppointInterval::query()->where('id',$row['appoint_time'])->first();
                    $row['appoint_time'] = $interval?$interval->start."-".$interval->end:'';
                    $row['status'] = \App\Models\Appoint::$stateTxt[$row['status']];

                    if(in_array($row['type'],[2,13])) {
                        $vaccine = Vaccine::query()->where('id', $row['project'])->first();
                        $row['vaccine'] = $vaccine?$vaccine->name:'';
                    }

                    $type = HospitalAppointType::query()->where('id',$row['type'])->first();
                    $row['type'] = $type?$type->name:'';
                    $hospital = Hospital::query()->where('id',$row['hospital_id'])->first();
                    $row['hospital'] = $hospital?$hospital->name:'';

                }
                return $rows;
            });

            $grid->filter(function (Grid\Filter $filter)use($types)  {

                $filter->equal('user_id');

                $filter->equal('type')->select($types);
                $filter->between('appoint_date')->date();
                $filter->between('created_at')->date();

                //$filter->equal('status','预约状态')->select(AppointModels::$stateTxt);
                $filter->where('status', function ($query) {

                    $query->where('eb_appoint.status', $this->input);

                },'预约状态')->select(AppointModels::$stateTxt);
                $filter->equal('hospital_id','社区医院')->select(Hospital::query()->pluck('name','id')->toArray());
                $filter->like('family.name','预约人姓名');
                $filter->where('phone', function (Builder $query) {
                    $query->orWhereHas('user',function (Builder $query){
                        $query->where('phone',$this->input);
                    });
                    $query->orWhereHas('family',function (Builder $query){
                        $query->where('phone',$this->input);
                    });

                },'手机号');

            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Appoint(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('family_id');
            $show->field('appoint_date');
            $show->field('appoint_time');
            $show->field('status');
            $show->field('hospital_id');
            $show->field('type');
            $show->field('project');
            $show->field('source');
            $show->field('id_card');
            $show->field('appoint_user_id');
            $show->field('cancel_appoint_user_id');
            $show->field('name');
            $show->field('cancel');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Appoint(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('family_id');
            $form->text('appoint_date');
            $form->text('appoint_time');
            $form->text('status');
            $form->text('hospital_id');
            $form->text('type');
            $form->text('project');
            $form->text('source');
            $form->text('id_card');
            $form->text('appoint_user_id');
            $form->text('cancel_appoint_user_id');
            $form->text('name');
            $form->text('cancel');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
